package com.luyou;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.Properties;

public class ExecuteDDL {

    private String driver;

    private String url;

    private String user;

    private String pass;

    public void initParam(String paramFile) throws Exception{

        //使用Properties类加载属性文件
        Properties props = new Properties();
        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(paramFile);
        props.load(is);
        driver = props.getProperty("driver");
        url = props.getProperty("url");
        user = props.getProperty("user");
        pass = props.getProperty("pass");
    }

    public void createTable(String sql)throws Exception{
        //加载驱动
        Class.forName(driver);
        try (
            Connection conn = DriverManager.getConnection(url, user, pass);
            Statement stmt = conn.createStatement())
        {
            //执行DDL语句,创建数据库表
            stmt.executeUpdate(sql);
        }
    }

    public static void main(String[] args) throws Exception {
        ExecuteDDL ed = new ExecuteDDL();
        ed.initParam("mysql.ini");
        ed.createTable(" create table jdbc_test "
        + "(jdbc_id int auto_increment primary key, "
                + " jdbc_name varchar(255), "
                + " jdbc_desc text )"
        );
        System.out.println("-----建表成功------");
    }
}
